<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/24
 * Time: 16:40
 */

namespace app\console\model;
use think\Model;
use think\Request;
use think\Db;

class SystemCard extends Model
{
    protected static function init()
    {

        // 写入前
        self::event('before_write', function ($data) {
                $interval_timer = explode(' / ', Request::instance()->param('interval_timer'));
//                $data->start_time = $interval_timer[0];
//                $data->end_time = $interval_timer[1];
                $data->create_time = date('Y-m-d H:i:s',time());
            $data->single_pic = uploadImg('single_pic');
            auding($data);
//            $data->single_pic = uploadImg('single_pic');
//                if(Request::instance()->param('type') == 1){
//                    $data->count = 10000;
//                    $type_name = '年卡';
//                }else{
//                    $data->count = Request::instance()->param('count');
//                    $data->minus_count = Request::instance()->param('count');
//                    $type_name = '次卡';
//                }
                //(new \app\index\controller\MemberMessage())->jpushMessage(Request::instance()->param()['member_id'],'恭喜您获得会员卡('.$type_name.')一张',Request::instance()->param()['conetnt'],3);


        });

        self::event('after_write', function ($data) {

            $param = Request::instance()->param();
            if(!empty($param['single_id'])){
                Db::name('system_card_count')->where('gift_id',$data->id)->delete();
            }
            $insertAll = [];
            if(!empty($param['single_id'])){ //服务类id
                for ($i = 0; $i < count($param['single_id']); $i++) {
                    $insertAll[$i]['gift_id'] = $data->id;
                    $insertAll[$i]['single_content'] = $param['single_content'][$i];
                    $insertAll[$i]['single_id'] = $param['single_id'][$i];
                    $insertAll[$i]['gift_count'] = $param['gift_count'][$i];
                }
                (new SystemCardCount())->saveAll($insertAll);
            }

        });
    }
    public function getCountNumberAttr($value, $data)
    {
        $number = Db::name('system_card_count')->where('gift_id',$data['id'])->sum('gift_count');
        return $number;
    }

    public function getAudingTextAttr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status']];
    }
    public function getAudingText1Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status1']];
    }
    public function getAudingText2Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status2']];
    }
    public function getAudingText3Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status3']];
    }
}